﻿using System;
using System.Collections.Generic;
using System.Linq;
using Missbot.Service;
using Dolany.UtilityTool;
using MongoDB.Bson.Serialization.Attributes;

namespace Missbot.AIModule.Vip
{
    public class VipArmerRecord : DbBaseEntity
    {
        public string UserID { get; set; }

        public List<ArmerModel> Armers { get; init; } = new();
    }

    public static class VipArmerDA
    {
        public static VipArmerRecord Get(string UserID)
        {
            var rec = MongoService<VipArmerRecord>.GetOnly(p => p.UserID == UserID);
            rec?.Armers.Remove(p => p.ExpiryTime != null && p.ExpiryTime < DateTime.Now);
            return rec;
        }

        public static bool CheckArmer(this VipArmerRecord entity, string armerName, int count = 1)
        {
            return entity.Armers.Count(a => a.Name == armerName) >= count;
        }

        public static void Update(this VipArmerRecord entity)
        {
            MongoService<VipArmerRecord>.Update(entity);
        }

        public static void Insert(this VipArmerRecord entity)
        {
            MongoService<VipArmerRecord>.Insert(entity);
        }
    }

    public class ArmerModel
    {
        public string Name { get; init; }

        public string Description { get; init; }

        [BsonDateTimeOptions(Kind = DateTimeKind.Local)]
        public DateTime? ExpiryTime { get; init; }
    }
}